//
//  CommentController.m
//  JZRJAPP
//
//  Created by mac on 16/5/31.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "CommentController.h"
#import "CommentCell.h"
#import "SettingViewController.h"
#import "NetWorkManager.h"
#import "LineView.h"
#import "CommentModel.h"
@interface CommentController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong) UIView *topView;

@property (nonatomic,strong)UIButton *leftBtn;

@property (nonatomic,strong)UILabel *titleLabel;

// 下面的tableView

@property (nonatomic,strong)UITableView *tableView;

@property (nonatomic,strong)UIView *titleView;

@property (nonatomic,strong)UIView *bottomView;

@property (nonatomic,strong)UITextField *textfile;

@property (nonatomic,strong)NSMutableArray *commentArray;

@property (nonatomic,strong)UITapGestureRecognizer *tapGestureRecognizer;
@end   

static NSString *indentFile = @"cellComents";
@implementation CommentController

- (void)viewDidLoad {
    [super viewDidLoad];
    //请求数据
    [self loadateComment:@"http://abiao123.xicp.net/LHWeb/Api/GetCommentData?code=002709.SZ&pageNum=1&pageCount=10"];
    
    self.view.backgroundColor = [UIColor cyanColor];
    self.topView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 64)];
    _topView.backgroundColor =[UIColor redColor];
    self.leftBtn =[UIButton buttonWithType:UIButtonTypeCustom];
    _leftBtn.frame =CGRectMake(20, 20, 30, 30) ;
    
    [_leftBtn setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
    [_leftBtn addTarget:self action:@selector(back:) forControlEvents:UIControlEventTouchUpInside];
    
    self.titleLabel =[[UILabel alloc]initWithFrame:CGRectMake(SCREEN_WIDTH / 2 -50, 20, 100, 40)];
    self.titleLabel.text = @"评论";
    self.titleLabel.font = [UIFont systemFontOfSize:20];
    self.titleLabel.textAlignment = NSTextAlignmentCenter;
    self.titleLabel.textColor = [UIColor whiteColor];
    
    
    

    self.tableView =[[UITableView alloc]initWithFrame:CGRectMake(0, 144, SCREEN_WIDTH, self.view.bounds.size.height) style:UITableViewStylePlain];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    [self.tableView registerNib:[UINib nibWithNibName:@"CommentCell" bundle:nil] forCellReuseIdentifier:indentFile];
    
    
    [self.view addSubview:self.tableView];
    self.tableView.backgroundColor =[UIColor yellowColor];
    
   
   
  
    
    // 自定义的view
    LineView *lineView =[[LineView alloc]initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, 80)];
    
    NSDate *currentDate = [NSDate date];//获取当前时间，日期
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"MM月dd日"];
    NSString *dateString = [dateFormatter stringFromDate:currentDate];
    NSString *textStr = @"竞猜中...";
    NSString *str = [dateString stringByAppendingFormat:@"%@",textStr];
    lineView.bottomLabelStr = str;
    
    [self.view addSubview:lineView];
    
    [self.topView addSubview:self.titleLabel];
    
    [self.topView addSubview:self.leftBtn];
    self.bottomView =[[UIView alloc]initWithFrame:CGRectMake(0, SCREEN_HEIGHT - 50, SCREEN_WIDTH, 50)];
    self.bottomView.backgroundColor =[UIColor cyanColor];
    UILabel *label =[[UILabel alloc]initWithFrame:CGRectMake(10, 10, 100, 30)];
    label.text = @"聊聊看法...";
    label.textColor = [UIColor grayColor];
    
    UIButton *rightBtn =[UIButton buttonWithType:UIButtonTypeCustom];
    
    rightBtn.frame =CGRectMake(SCREEN_WIDTH - 50, 10, 30, 30);
    [rightBtn setImage:[UIImage imageNamed:@"index_off"] forState:UIControlStateNormal];
    
     self.tapGestureRecognizer =[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap:)];
     [self.bottomView addGestureRecognizer:_tapGestureRecognizer];
    
    [self.bottomView addSubview:rightBtn];
    
    [self.bottomView addSubview:label];
    
    
 //   self.tableView.tableHeaderView = lineView;
    [self.view addSubview:self.bottomView];
    
    [self.view addSubview:self.topView];
    

   }
-(void)loadateComment:(NSString *)url{
    
    [NetWorkManager getUrl:url paramdic:nil success:^(id data) {
        
        NSMutableArray *dataArray = [NSMutableArray array];
        
        for (NSDictionary *dic in data) {
            CommentModel *commentModel = [CommentModel new];
            [commentModel setValuesForKeysWithDictionary:dic];
            [dataArray addObject:commentModel];
            
        }
        self.commentArray = dataArray.mutableCopy;
        [self.tableView reloadData];

    } error:^(id error) {
        NSLog(@"error");
        
    }];
}

-(void)tap:(UITapGestureRecognizer *)tap{
    
    NSLog(@"弹出一个视图");
    UIView *aview =[[UIView alloc]initWithFrame:CGRectMake(20, SCREEN_HEIGHT/ 2 - 100, SCREEN_WIDTH - 40 , 200)];
    
    aview.backgroundColor =[UIColor grayColor];
    
    UITextField *textView = [[UITextField alloc]initWithFrame:CGRectMake(10, 10, aview.frame.size.width - 20, 150)];
    
    textView.backgroundColor =[UIColor whiteColor];
    
    textView.placeholder = @"请输入你的看法";
    textView.font =[UIFont systemFontOfSize:20];
    UIButton *buttonleft = [UIButton buttonWithType:UIButtonTypeCustom];
    
    buttonleft.frame =CGRectMake(10, 160, (aview.frame.size.width -20)/2, 30);
    
    [buttonleft setTitle:@"确定" forState:UIControlStateNormal];
    
    [buttonleft setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    
    [aview addSubview:buttonleft];
    [aview addSubview:textView];
    
    [self.view addSubview:aview];
    
    
}
-(NSMutableArray *)commentArray{
    if (!_commentArray) {
        _commentArray =[NSMutableArray array];
    }
    return _commentArray;
}
#pragma mark 代理
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.commentArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    CommentCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellComents" forIndexPath:indexPath];
    
   
    cell.model = self.commentArray[indexPath.row];
    cell.textLabel.text = @"123";
    
    return cell;
    
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 150;
}
-(void)back:(UIButton *)sender{
    [self dismissViewControllerAnimated:YES completion:nil];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
